package com.pf.utils;

import com.fasterxml.jackson.databind.ObjectMapper;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.util.HashMap;
import java.util.Map;
import java.util.UUID;

public class Test {

    @Data
    @NoArgsConstructor
    @AllArgsConstructor
    @Builder
    public static class ParamDTO{
        private String aesKey;
        private String param;
    }

    public static void main(String[] args) throws Exception {
        String appId = "gb2jwpgFaLtEysCSwO3s72xX";
        String appSecret = "7a8yHdWEdnpSvvUQtRdV2hEd";
        String rsaPublicKey = "MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAu843WdnIeDzcbSWxjUkTEvN4KNMnvVLjD0OoMHDanxkMbimSN9EMZ5KCxmD9fF5qeYkl2GQjaP92pHLXgwkfIOrMqKgsRAOCJyV9pfVfcK0/j9Bra+AOEZcWeL9FrxKHZ9GspYBLV4b5T0FqZ+STWZhVzLuByOn35upNQVGmslAnH5F3ffino6/7vBTkAldIwUQKK1RqggSV3SNXPK/Kuo+SyKf3nW9cQNts0jXYdIBsup8XCLf2fn1sAqVt6w9VHB8Ih+STc/JqAnPtRscHPXvmEMk/itvFaWUGuGWDntx3wljzRm9iVDWGSHuWa/TQAikZ49WBOWuGpvv8jagA8wIDAQAB";

        ObjectMapper om = new ObjectMapper();
        String reqParam = "{\"data\":[{\"indemnityNo\":\"41899004100318545674\",\"receiptNo\":\"18050241000143706875\",\"buyerLevel3Code\":\"0530\",\"sellerName\":\"渝北区区区汽车修理厂\",\"payTime\":\"2022-01-19\",\"amount\":3882.0,\"licenseNo\":\"渝A517CV\",\"noticeNo\":\"18050241000143706875\",\"feeType\":\"REPAIR_FEE\",\"buyerTerritory\":\"LOCAL\",\"caseNo\":\"91800002700136800602\",\"sellerCode\":\"218998619551\",\"buyerSecondaryCode\":\"05\"}],\"trxId\":\"7c948839-c925-4126-a69b-5a14a5db6456\",\"trxSndDT\":\"20220225164544\",\"channelNo\":\"DJCX\"}";

        String aesKey = EAESUtils.getAesKey();
        String encryptParam = EAESUtils.encrypt(reqParam, aesKey);

        String encryptAesKey = ERSAUtils.encrypt(aesKey, ERSAUtils.getPublicKey(rsaPublicKey));

        String requestId = UUID.randomUUID().toString();
        String timestamp = System.currentTimeMillis() + "";
        Map<String, String> req = new HashMap<>();
        req.put("APP-ID", appId);
        req.put("APP-REQUEST-ID", requestId);
        req.put("APP-TIMESTAMP", timestamp);
        String sign = ESignUtils.sign(appSecret, req, encryptParam);

        System.out.println("APP-ID="+appId);
        System.out.println("APP-REQUEST-ID="+requestId);
        System.out.println("APP-TIMESTAMP="+timestamp);
        System.out.println("APP-SIGN="+sign);

        ParamDTO build = ParamDTO.builder().aesKey(encryptAesKey).param(encryptParam).build();
        System.out.println(om.writeValueAsString(build));

    }
}
